home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
9-Digit Zip Code Directory
/
9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO
/
z4src.zip
/
BSFILE2.C
< prev
next >
Wrap
C/C++ Source or Header
|
1995-08-04
|
5KB
|
172 lines
//----------------------------------------------------------------------------
// MODULE DESCRIPTION
//
// Module: bsfile2.c
// Title: Base library
// Notice: John M. Weeder
// Copyright (c) 1993. All rights reserved.
// This module contains proprietary information and should be
// treated as confidential.
//
//----------------------------------------------------------------------------
// MAINTENANCE HISTORY
//
// $Workfile$
// $Revision$
// $Author$
// $Date$
// $Log$
//
//----------------------------------------------------------------------------
// MODULE NARRATIVE
//
//
// This module contains code to read text files. It roughly approximates
// fgets() and the other streaming functions. However, this functions use
// the base library file function so that critical error and stuff are
// handled correctly.
//
// This class is a prime candidate for C++ however it is needed on the
// SCO UNIX platform to read configuration files.
//
// The code in this module should be written entirely in C.
// Do not use any C++ constructs.
//
//
// The code in this module should be written entirely in C.
// Do not use any C++ constructs.
//
// This module is portable to:
// DOS 3.X+
// MS Windows 3.X+
// MS Windows NT
// OS/2 2.X+
// OS/2 2.0 PM
// SCO UNIX.
//
// The following compilers are supported:
// MSC 6.0A
// MSC/C++ 7.0
// Borland C++ 3.1 for DOS
// Borland C++ 1.0 for OS/2 2.X
// SCO UNIX cc
//
//----------------------------------------------------------------------------
#include <bs.h>
//----------------------------------------------------------------------------
// Description: Get file name.
// Parameters: hf File handle.
// Returns: Pointer to file name
//----------------------------------------------------------------------------
ULONG FN_E FileLine(HF hf)
{
PBS_HF pbs_hf = FileHandle(hf);
return pbs_hf == NULL ? 0L: pbs_hf->ulLine;
}
//----------------------------------------------------------------------------
// Description: Get file name.
// Parameters: hf File handle.
// Returns: Pointer to file name
//----------------------------------------------------------------------------
PCSZ FN_E FileName(HF hf)
{
PBS_HF pbs_hf = FileHandle(hf);
return pbs_hf == NULL || !pbs_hf->pcszName[0] ? "unknown": pbs_hf->pcszName;
}
//----------------------------------------------------------------------------
// Description: Read a line of text from an input file.
// Parameters: hf File handle
// pch Buffer to receive text
// cch Size of buffer.
// Returns: TRUE if successful and a line of text was found.
//----------------------------------------------------------------------------
BOOL FN_E FileReadStr(HF hf, PCHAR pch, SIZET cch)
{
PBS_HF pbs_hf = FileHandle(hf);
Assert(pch && cch);
if (pbs_hf == NULL
|| pbs_hf->fError
|| ((pbs_hf->fsize - pbs_hf->fpos) <= 0
&& (!pbs_hf->cDat || pbs_hf->cOff >= pbs_hf->cDat)))
return FALSE;
if (!BTEST(pbs_hf->fl, FL_BUFFERED))
return FileError(hf, "File is not buffered.");
pbs_hf->ulLine++;
while (cch > 1)
{ // Nothing left?
FPOS ffile = pbs_hf->fsize - pbs_hf->fpos;
if (ffile <= 0
&& (!pbs_hf->cDat || pbs_hf->cOff >= pbs_hf->cDat))
break;
if (!pbs_hf->cDat // Buffer empty
|| pbs_hf->cOff >= pbs_hf->cDat) // Or buffer used up
{
SIZET cRead; // Read another buffer full
cRead = (SIZET)MIN((FPOS)pbs_hf->cBuf, (FPOS)ffile);
if (!FileRead(hf, (PSZ)pbs_hf->pcszBuf, cRead, (FPOS)-1L))
{
pbs_hf->fError = TRUE;
return FALSE;
}
pbs_hf->cDat = cRead; // Adjust globals
pbs_hf->cOff = 0;
}
for (; pbs_hf->cOff < pbs_hf->cDat && cch > 1; ++pbs_hf->cOff)
if (pbs_hf->pcszBuf[pbs_hf->cOff] == '\n')
{
pbs_hf->cOff++;
goto EXIT_HERE;
}
else if (pbs_hf->pcszBuf[pbs_hf->cOff] != '\r')
{
*pch++ = pbs_hf->pcszBuf[pbs_hf->cOff];
cch--;
}
}
EXIT_HERE:
pch[0] = '\0';
return TRUE;
}
//----------------------------------------------------------------------------
// Description: Run standard test suite
// Parameters: sTest Test to run.
// 0 Run all default tests (except).
// Returns: TRUE if successful.
//----------------------------------------------------------------------------
#if COMPILE_TEST
BOOL FN TextInTest(SHORT sTest)
{
HF hf;
CHAR szBuffer[256];
NOTUSED(sTest);
Output("Read config.sys as text file:\n");
if (!FileOpen(&hf, "c:\\config.sys", FL_O_RT, NULL))
return FALSE;
while (FileReadStr(hf, szBuffer, sizeof(szBuffer)))
Output(" %05ld: [%s]\n", FileLine(hf), szBuffer);
FileClose(hf);
return TRUE;
}
#endif
//----------------------------------------------------------------------------
//------------------------------- End of File --------------------------------
//----------------------------------------------------------------------------